package cn.bluech.lib.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;

import cn.bluech.lib.BuildConfig;

/**
 * Description:
 * Create by Codeant on 2019/9/3.
 */
public class AppUtils {
    /**
     * 获取应用程序名称
     */
    @NonNull
    public static String getName(Context ctx) {
        try {
            PackageManager manager = ctx.getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(
                    ctx.getPackageName(),0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return ctx.getResources().getString(labelRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getName(Context ctx, String path){
        PackageManager pm = ctx.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(path,PackageManager.GET_ACTIVITIES);
        if(null==info)
            return "";

        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = path;
        appInfo.publicSourceDir = path;

        return appInfo.loadLabel(pm).toString();
    }

    /**
     * 获取应用程序版本名称
     * use BuildConfig.VERSION_NAME
     */
    @Deprecated
    public static String getVerName(Context ctx) {
        try {
            PackageManager manager = ctx.getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(
                    ctx.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getVerName(Context ctx, String path){
        PackageManager pm = ctx.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(path,PackageManager.GET_ACTIVITIES);
        if(null==info)
            return "";

        return null!=info.versionName?info.versionName:"";
    }

    /**
     * 获取应用程序版本号
     * use BuildConfig.VERSION_CODE
     */
    @Deprecated
    public static int getVersionCode(Context ctx) {
        try {
            PackageManager manager = ctx.getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(
                    ctx.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取应用程序包名
     * use BuildConfig.APPLICATION_ID
     */
    @Deprecated
    public static String getPackageName(Context ctx) {
        try {
            PackageManager manager = ctx.getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(
                    ctx.getPackageName(), 0);
            return packageInfo.packageName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getPackageName(Context ctx, String path){
        PackageManager pm = ctx.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(path,PackageManager.GET_ACTIVITIES);
        if(null==info)
            return "";

        return info.applicationInfo.packageName;
    }

    /**
     * 获取应用图标 bitmap
     */
    public static Bitmap getIcon(Context ctx) {
        PackageManager manager = null;
        ApplicationInfo applicationInfo = null;
        try {
            manager = ctx.getApplicationContext().getPackageManager();
            applicationInfo = manager.getApplicationInfo(
                    ctx.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
        Drawable drawable = manager.getApplicationIcon(applicationInfo);

        return ((BitmapDrawable) drawable).getBitmap();
    }

    public static Drawable getIcon(Context ctx, String path){
        PackageManager pm = ctx.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(path,PackageManager.GET_ACTIVITIES);
        if(null==info)
            return null;

        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = path;
        appInfo.publicSourceDir = path;

        return appInfo.loadIcon(pm);
    }

    public static boolean isInstalled(Context ctx, String path){
        PackageManager pm = ctx.getPackageManager();
        try {
            pm.getPackageInfo(getPackageName(ctx,path),PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
